package cn.tp.ksoft.civil_driver.module.login;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;

import java.io.File;

import cn.tp.ksoft.civil_driver.DriverBaseActivity;
import cn.tp.ksoft.civil_driver.R;
import cn.tp.ksoft.civil_driver.bean.ResultBean;
import cn.tp.ksoft.civil_driver.dialog.DialogCallBack;
import cn.tp.ksoft.civil_driver.dialog.RegisterFinishDialog;
import cn.tp.ksoft.civil_driver.request.DriverRequest;
import cn.tp.ksoft.civil_driver.utils.Utils;

/**
 * Created by zhusong on 16/5/1.
 */
public class VerifyCardsActivity extends DriverBaseActivity implements View.OnClickListener{
    private final int REQUEST_TAKE_TRAVEL = 1;
    private final int REQUEST_TAKE_DRIVE = 2;

    private View mTravelView;
    private View mDriveView;

    private ImageView mTravelCardIv;
    private ImageView mDriverCardIv;
    private RegisterFinishDialog mDialog;

    private DriverRequest request;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.driver_activity_verify_driver_cards);
        updateTitle(getString(R.string.driver_reg_finish));

        findViewById(R.id.btn_take_travel_card).setOnClickListener(this);
        findViewById(R.id.btn_take_driver_card).setOnClickListener(this);
        findViewById(R.id.btn_close_travel_card).setOnClickListener(this);
        findViewById(R.id.btn_close_driver_card).setOnClickListener(this);

        mTravelView = findViewById(R.id.rl_travel_card);
        mDriveView = findViewById(R.id.rl_driver_card);
        mTravelCardIv = (ImageView) findViewById(R.id.iv_travel_card);
        mDriverCardIv = (ImageView) findViewById(R.id.iv_driver_card);

        request = new DriverRequest(this, new WeakHandler(this));
        findViewById(R.id.btn_submit).setOnClickListener(this);
        mDialog = new RegisterFinishDialog(this);
        mDialog.setCallBack(new DialogCallBack() {
            @Override
            public void onPositiveClick(Object... result) {
                setResult(RESULT_OK);
                finish();
            }

            @Override
            public void onNegativeClick(Object... result) {
            }
        });
    }
    private File travelFile;
    private File driveFile;

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == REQUEST_TAKE_TRAVEL) {
            if (resultCode == RESULT_OK) {
                Uri uri = data.getData();
                mTravelCardIv.setImageURI(uri);
                String filePath = Utils.getImageAbsolutePath(this, uri);
                if (TextUtils.isEmpty(filePath) || !(travelFile = new File(filePath)).exists()) {
                    Toast.makeText(this, "图片未找到", Toast.LENGTH_SHORT).show();
                    return;
                }
                mTravelView.setVisibility(View.VISIBLE);
            }
        } else if (requestCode == REQUEST_TAKE_DRIVE) {
            if (resultCode == RESULT_OK) {
                Uri uri = data.getData();
                mDriverCardIv.setImageURI(uri);
                String filePath = Utils.getImageAbsolutePath(this, uri);
                if (TextUtils.isEmpty(filePath) || !(driveFile = new File(filePath)).exists()) {
                    Toast.makeText(this, "图片未找到", Toast.LENGTH_SHORT).show();
                    return;
                }
                mDriveView.setVisibility(View.VISIBLE);
            }
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_take_travel_card : {
                Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
                intent.setType("image/*");
                startActivityForResult(intent, REQUEST_TAKE_TRAVEL);
                break;
            }

            case R.id.btn_take_driver_card : {
                Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
                intent.setType("image/*");
                startActivityForResult(intent, REQUEST_TAKE_DRIVE);
                break;
            }

            case R.id.btn_close_travel_card : {
                travelFile = null;
                mTravelView.setVisibility(View.GONE);
                break;
            }

            case R.id.btn_close_driver_card : {
                driveFile = null;
                mDriveView.setVisibility(View.GONE);
                break;
            }

            case R.id.btn_submit : {
                if (travelFile == null) {
                    Toast.makeText(this, getString(R.string.driver_please_take_car_travel_card),
                            Toast.LENGTH_SHORT).show();
                    return;
                }
                if (driveFile == null) {
                    Toast.makeText(this, getString(R.string.driver_please_take_car_drive_card),
                            Toast.LENGTH_SHORT).show();
                    return;
                }
                request.setLicense(travelFile, driveFile);
                break;
            }
        }
    }

    @Override
    protected void handleSuccessResult(int what, @NonNull ResultBean result) {
        switch (what) {
            case SETLICENSE : {
                mDialog.show();
                break;
            }
        }
    }
}
